package com.yao.club.mapper;

import com.yao.club.entity.vo.PostsInfoVO;
import com.yao.club.entity.dto.InformationDetail;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.mapstruct.Mapper;

import java.util.List;

@Mapper
public interface InformationMapper {
    @Select("select * from information_detail where informationId =#{informationId}")
    InformationDetail select(@Param("informationId") String informationId);


    @Update("UPDATE posts SET comment_number = comment_number + 1 WHERE  informationId= #{informationId}")
    int incrementCommentCount(@Param("informationId") String informationId);

    @Update("UPDATE posts SET likeNumber = likeNumber + 1 WHERE  postsId= #{postsId}")
    int incrementLikeCount(@Param("postsId") Long postsId);

    @Update("UPDATE posts SET likeNumber = likeNumber - 1 WHERE  postsId= #{postsId}")
    int unIncrementLikeCount(@Param("postsId") Long postsId);

    @Update("UPDATE posts SET shareNumber = shareNumber + 1 WHERE  postsId= #{postsId}")
    int incrementShareCount(@Param("postsId") Long postsId);

    @Update("UPDATE posts SET shareNumber = shareNumber - 1 WHERE  postsId= #{postsId}")
    int unIncrementShareCount(@Param("postsId") Long postsId);


    @Update("UPDATE posts SET collectNumber = collectNumber + 1 WHERE  postsId= #{postsId}")
    int incrementCollectCount(@Param("postsId") Long postsId);

    @Update("UPDATE posts SET collectNumber = collectNumber - 1 WHERE  postsId= #{postsId}")
    int unIncrementCollectCount(@Param("postsId") Long postsId);

    /**
     * 统计未被指定用户屏蔽的有效帖子数量
     */
    @Select("SELECT COUNT(*) FROM posts p " +
            "WHERE p.delFlag = 0  " +
            "AND NOT EXISTS (SELECT 1 FROM posts_shield_record s WHERE s.post_id = p.postsId AND s.user_id = #{userId})")
    int countPostsExcludeShielded(@Param("userId") String userId);

    /**
     * 统计搜索结果总数（排除屏蔽的帖子）
     */
    @Select("SELECT COUNT(*) FROM posts p " +
            "WHERE p.delFlag = 0  " +
            "AND (p.content LIKE CONCAT('%', #{keyword}, '%') " +
            "OR EXISTS (SELECT 1 FROM posts_topic t " +
            "           WHERE FIND_IN_SET(t.topicId, p.topicIds) " +
            "           AND t.topicName LIKE CONCAT('%', #{keyword}, '%'))) " +
            "AND NOT EXISTS (SELECT 1 FROM posts_shield_record s " +
            "               WHERE s.post_id = p.postsId " +
            "               AND s.user_id = #{userId})")
    int countSearchPostsExcludeShielded(@Param("keyword") String keyword,
                                        @Param("userId") String userId);
    /**
     * 统计话题下的帖子数量（排除屏蔽的帖子）
     */
        @Select("SELECT COUNT(*) FROM posts p " +
                "WHERE p.delFlag = 0  " +
            "AND FIND_IN_SET(#{topicId}, p.topicIds) " +
            "AND NOT EXISTS (SELECT 1 FROM posts_shield_record s " +
            "               WHERE s.post_id = p.postsId " +
            "               AND s.user_id = #{userId})")
    int countTopicPostsExcludeShielded(@Param("topicId") String topicId, 
                                      @Param("userId") String userId);

    /**
     * 分页查询话题下的帖子（排除屏蔽的帖子）
     */
    @Select({
        "<script>",
        "SELECT p.* FROM posts p ",
          "WHERE p.delFlag = 0  " +
        "AND FIND_IN_SET(#{topicId}, p.topicIds) ",
        "AND NOT EXISTS (SELECT 1 FROM posts_shield_record s ",
        "               WHERE s.post_id = p.postsId ",
        "               AND s.user_id = #{userId}) ",
        "<if test='sortType == \"hot\"'>",
        "ORDER BY (p.like_count * 2 + p.comment_count * 3 + p.collect_count * 4 + p.share_count) DESC, p.create_time DESC",
        "</if>",
        "<if test='sortType != \"hot\"'>",
        "ORDER BY p.create_time DESC",
        "</if>",
        "LIMIT #{offset}, #{pageSize}",
        "</script>"
    })
    List<PostsInfoVO> pageTopicPostsExcludeShielded(@Param("topicId") String topicId,
                                                    @Param("userId") String userId,
                                                    @Param("offset") int offset,
                                                    @Param("pageSize") int pageSize,
                                                    @Param("sortType") String sortType);

    /**
     * 统计话题下的所有有效帖子数量
     */
    @Select("SELECT COUNT(*) FROM posts p " +
            "WHERE p.delFlag = 0  " +
            "AND FIND_IN_SET(#{topicId}, p.topicIds)")
    int countTopicAllValidPosts(@Param("topicId") String topicId);

    /**
     * 分页查询话题下的所有有效帖子
     */
    @Select({
        "<script>",
        "SELECT p.* FROM posts p ",
        "WHERE p.delFlag = 0  " +
        "AND FIND_IN_SET(#{topicId}, p.topicIds) ",
        "<if test='sortType == \"hot\"'>",
        "ORDER BY (p.like_count * 2 + p.comment_count * 3 + p.collect_count * 4 + p.share_count) DESC, p.create_time DESC",
        "</if>",
        "<if test='sortType != \"hot\"'>",
        "ORDER BY p.create_time DESC",
        "</if>",
        "LIMIT #{offset}, #{pageSize}",
        "</script>"
    })
    List<PostsInfoVO> pageTopicAllValidPosts(@Param("topicId") String topicId,
                                             @Param("offset") int offset,
                                             @Param("pageSize") int pageSize,
                                             @Param("sortType") String sortType);
    /**
     * 分页查询未被指定用户屏蔽的有效帖子
     */
    @Select({
            "<script>",
            "SELECT p.* FROM posts p ",
            "WHERE p.delFlag = 0 ",
            "AND NOT EXISTS (SELECT 1 FROM posts_shield_record s WHERE s.post_id = p.postsId AND s.user_id = #{userId}) ",
            "<if test='sortType == \"hot\"'>",
            "ORDER BY (p.like_count * 2 + p.comment_count * 3 + p.collect_count * 4 + p.share_count) DESC, p.create_time DESC",
            "</if>",
            "<if test='sortType != \"new\"'>",
            "ORDER BY p.create_time DESC",
            "</if>",
            "LIMIT #{offset}, #{pageSize}",
            "</script>"
    })
    List<PostsInfoVO> pagePostsExcludeShielded(@Param("userId") String userId,
                                               @Param("offset") int offset,
                                               @Param("pageSize") int pageSize,
                                               @Param("sortType") String sortType);

    /**
     * 搜索帖子（排除屏蔽的帖子）
     */
    @Select("SELECT p.* FROM posts p " +
            "WHERE p.delFlag = 0 "+
            "AND (p.content LIKE CONCAT('%', #{keyword}, '%') " +
            "OR EXISTS (SELECT 1 FROM posts_topic t " +
            "           WHERE FIND_IN_SET(t.topicId, p.topicIds) " +
            "           AND t.topicName LIKE CONCAT('%', #{keyword}, '%'))) " +
            "AND NOT EXISTS (SELECT 1 FROM posts_shield_record s " +
            "               WHERE s.post_id = p.postsId " +
            "               AND s.user_id = #{userId}) " +
            "ORDER BY p.create_time DESC " +
            "LIMIT #{offset}, #{pageSize}")
    List<PostsInfoVO> searchPostsExcludeShielded(@Param("keyword") String keyword,
                                                 @Param("userId") String userId,
                                                 @Param("offset") int offset,
                                                 @Param("pageSize") int pageSize);

    /**
     * 统计所有有效帖子数量
     */
    @Select("SELECT COUNT(*) FROM posts WHERE delFlag = 0")
    int countAllValidPosts();


    /**
     * 统计所有符合搜索条件的帖子数量
     */
    @Select("SELECT COUNT(*) FROM posts p " +
            "WHERE p.delFlag = 0 " +
            "AND (p.content LIKE CONCAT('%', #{keyword}, '%') " +
            "OR EXISTS (SELECT 1 FROM posts_topic t " +
            "           WHERE FIND_IN_SET(t.topicId, p.topicIds) " +
            "           AND t.topicName LIKE CONCAT('%', #{keyword}, '%')))")
    int countSearchAllValidPosts(@Param("keyword") String keyword);

    /**
     * 搜索所有符合条件的帖子
     */
    @Select("SELECT p.* FROM posts p " +
            "WHERE p.delFlag = 0 " +
            "AND (p.content LIKE CONCAT('%', #{keyword}, '%') " +
            "OR EXISTS (SELECT 1 FROM posts_topic t " +
            "           WHERE FIND_IN_SET(t.topicId, p.topicIds) " +
            "           AND t.topicName LIKE CONCAT('%', #{keyword}, '%'))) " +
            "ORDER BY p.create_time DESC " +
            "LIMIT #{offset}, #{pageSize}")
    List<PostsInfoVO> searchAllValidPosts(@Param("keyword") String keyword,
                                          @Param("offset") int offset,
                                          @Param("pageSize") int pageSize);

    /**
     * 分页查询所有有效帖子
     */
    @Select({
            "<script>",
            "SELECT * FROM posts ",
            "WHERE delFlag = 0 ",
            "<if test='sortType == \"hot\"'>",
            "ORDER BY (like_count * 2 + comment_count * 3 + collect_count * 4 + share_count) DESC, create_time DESC",
            "</if>",
            "<if test='sortType != \"hot\"'>",
            "ORDER BY create_time DESC",
            "</if>",
            "LIMIT #{offset}, #{pageSize}",
            "</script>"
    })
    List<PostsInfoVO> pageAllValidPosts(@Param("offset") int offset,
                                        @Param("pageSize") int pageSize,
                                        @Param("sortType") String sortType);



}
